package plugin.eos.awake.flutter.awake1.network;

import java.util.HashMap;

import io.reactivex.schedulers.Schedulers;
import plugin.eos.awake.flutter.awake1.BuildConfig;
import plugin.eos.awake.flutter.awake1.network.converter.MyGsonConverterFactory;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * Creation time: 2018/7/30
 * Editor: $USER_NAME
 * Functional description:
 */
public class RetrofitManager {

    private static Retrofit retrofit;
    private static HashMap<Class, Object> cachedApi = new HashMap<>();
    private static String HTTP_VERSION = "/v1/";
    private static void init() {

        retrofit = new Retrofit.Builder()
                .client(NetworkFactory.getOkHttpClient())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
                .baseUrl(baseURL())
                .addConverterFactory(MyGsonConverterFactory.create())
                .build();
    }

    private static String baseURL() {
        return BuildConfig.BASE_URL+HTTP_VERSION;
    }

    public static <T> T get(final Class<T> service) {
        if (retrofit == null) {
            init();
        }
        T api = retrofit.create(service);
        cachedApi.put(service, api);
        return api;
//		if (cachedApi.containsKey(service)) {
//			return (T) cachedApi.get(service);
//		} else {
//			T api = retrofit.create(service);
//			cachedApi.put(service, api);
//			return api;
//		}
    }
}
